/*
 * Copyright [2013-2021], Alibaba Group Holding Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.alibaba.polardbx.matrix.jdbc;

import com.alibaba.polardbx.common.jdbc.ParameterContext;
import com.alibaba.polardbx.common.jdbc.Parameters;
import com.alibaba.polardbx.common.logical.ITPrepareStatement;
import com.alibaba.polardbx.druid.sql.parser.ByteString;
import com.alibaba.polardbx.optimizer.OptimizerContext;
import com.alibaba.polardbx.optimizer.context.ExecutionContext;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author mengshi.sunmengshi 2013-11-22 下午3:26:18
 * @since 5.0.0
 */
public class TPreparedStatement extends TStatement implements ITPrepareStatement {

    // 参数列表到参数上下文的映射 如 1:name 2：'2011-11-11'
    protected Parameters params = new Parameters();

    public TPreparedStatement(TDataSource ds, TConnection conn, ByteString sql, ExecutionContext ec) {
        super(ds, conn, sql, ec);
    }

    @Override
    public ResultSet getResultSet() throws SQLException {
        return updateCount < 0 ? currentResultSet : null;
    }

    @Override
    public int getUpdateCount() throws SQLException {
        if (currentResultSet instanceof TMultiResultSet) {
            return ((TMultiResultSet) currentResultSet).getAffectRows();
        } else {
            return updateCount;
        }
    }

    // jdbc规范: 返回true表示executeQuery，false表示executeUpdate
    @Override
    public boolean execute() throws SQLException {
        checkClosed();
        ensureResultSetIsEmpty();

        OptimizerContext.setContext(this.ds.getConfigHolder().getOptimizerContext());
        executionContext.setAutoGeneratedKeys(-1);
        executionContext.setColumnIndexes(null);
        executionContext.setColumnNames(null);
        executeSQL(sql);
        if (currentResultSet instanceof TMultiResultSet) {
            updateCount = ((TMultiResultSet) currentResultSet).getAffectRows();
        } else if (currentResultSet instanceof TResultSet) {
            updateCount = ((TResultSet) currentResultSet).getAffectRows();
        } else {
            updateCount = -1;
        }
        // 如果是读操作,updateCount=-1,返回true
        return updateCount < 0;
    }

    @Override
    public ResultSet executeQuery() throws SQLException {
        return this.executeQuery(sql);
    }

    @Override
    protected void executeSQL(ByteString sql) throws SQLException {
        currentResultSet = conn.executeSQL(sql, params, this, executionContext);
    }

    public void setParam(int parameterIndex, ParameterContext context) {
        params.getCurrentParameter().put(parameterIndex, context);
    }

    public void addBatch() throws SQLException {
        params.addBatch();
    }
}
